<?php

class Compte extends FrontEnd_Controller {

    function Compte() {
        parent::FrontEnd_Controller();
        $this->_data['erreur'] = "";
        $this->load->model('Inscription_m');
    }
    function index(){
         is_adherent();
        $this->enregistrer();
    }
    function enregistrer(){
         is_adherent();
        $this->_data['page']="Mon compte";
        $iduser=$this->session->userdata('userid');
         $info_user=$this->Inscription_m->get_adherent_by_id($iduser);

         $nom_photo1="";
         $nom_photo2="";
    

        $config = array(
            array(
                'field' => 'civile',
                'rules' => 'required'
            ),
            array(
                'field' => 'nom',
                'rules' => 'required'
            ),
            array(
                'field' => 'prenom',
                'rules' => 'required'
            ),
            array(
                'field' => 'profession',
                'rules' => 'required'
            ),
            array(
                'field' => 'statut',
                'rules' => 'required'
            ),
            array(
                'field' => 'raison_ent',
                'rules' => 'required'
            ),
            array(
                'field' => 'siret',
                'rules' => 'required|numeric'
            ),
            array(
                'field' => 'adr',
                'rules' => 'required'
            ),
            array(
                'field' => 'postale',
                'rules' => 'required|numeric|exact_length[5]'
            ),
            array(
                'field' => 'ville',
                'rules' => 'required'
            ),
            array(
                'field' => 'site',
                'rules' => 'url'
            ),
            array(
                'field' => 'tel',
                'rules' => 'required|numeric|min_length[10]'
            ),
            array(
                'field' => 'portable',
                'rules' => 'numeric|min_length[10]'
            ),
            array(
                'field' => 'sport[]',
                'rules' => 'required'
            ),
            array(
                'field' => 'recherche[]',
                'rules' => 'required'
            ),
            array(
                'field' => 'secteur',
                'rules' => 'required'
            ),
        );

        $this->_data['sports'] = $this->Inscription_m->get_sports();
        $this->_data['recherche'] = $this->Inscription_m->get_recherche();
        $this->_data['secteur'] = $this->Inscription_m->get_secteur();
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/img_web_ents/';
                $thumb_path=$image_path.'thumbs';
                $config['upload_path'] = $image_path;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';
                $this->load->library('upload');
                 $this->load->library('image_lib');
                $this->upload->initialize($config);

                if($this->upload->do_upload('logo_ent')){
                 
                        $image_to_delete = './assets/img_web_ents/' . $info_user->logo_ent;
                        $thumb_to_delete = './assets/img_web_ents/thumbs/' . $info_user->logo_ent;
             

                 $upload_info = $this->upload->data();
                 $nom_photo1=$upload_info['file_name'];
                   $config = array(
                        'source_image' => $upload_info['full_path'],
                        'new_image' => $thumb_path,
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                } else {
                $nom_photo1=$info_user->logo_ent;
                }


                 $image_path1 = './assets/img_web_ads/';
                 $thumb_path1 = $image_path1 . '/thumbs';
                $config['upload_path'] = $image_path1;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';

          $this->upload->initialize($config);
          if($this->upload->do_upload('logo_ad')) {


                        $image_to_delete = './assets/img_web_ads/' . $info_user->logo_ad;
                        $thumb_to_delete = './assets/img_web_ads/thumbs/' . $info_user->logo_ad;
                        delete_image($image_to_delete, $thumb_to_delete);
               


             $upload_info1 = $this->upload->data();
             $nom_photo2=$upload_info1['file_name'];
                   $config = array(
                        'source_image' => $upload_info1['full_path'],
                        'new_image' => $thumb_path1,
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
          } else  $nom_photo2=$info_user->logo_ad;

        $sport="";$recherche="";
         $asport=$this->input->post('sport');
         $N = count($asport);
         for($i=0; $i < $N; $i++)
         { if ($asport[$i]!="")$sport=$sport.','.$asport[$i];}
         $sport = substr($sport, 1);
        
         $arecherche=$this->input->post('recherche');
         $N = count($arecherche);
         for($j=0; $j < $N; $j++)
         { if ($arecherche[$j]!="")$recherche=$recherche.','.$arecherche[$j];}
       $recherche = substr($recherche, 1);
         $data = array(

             'civille'=> $this->input->post('civile'),
             'nom'=> $this->input->post('nom'),
             'prenom'=> $this->input->post('prenom'),
             'profession'=> $this->input->post('profession'),
             'statut'=> $this->input->post('statut'),
             'raison_ent'=> $this->input->post('raison_ent'),
             'sport'=> $sport,
             'pratique'=> $this->input->post('pratique'),
             'precis'=> $this->input->post('precision'),
             'recherche'=> $recherche,
             'siret'=> $this->input->post('siret'),
            'date_creation'=> $this->input->post('date_creation'),
             'effectif'=> $this->input->post('effectif'),
             'adr'=> $this->input->post('adr'),
             'postale'=> $this->input->post('postale'),
             'ville'=> $this->input->post('ville'),
             'site'=> $this->input->post('site'),
             'tel'=> $this->input->post('tel'),
             'portable'=> $this->input->post('portable'),
             'logo_ent'=> $nom_photo1,
             'logo_ad'=>  $nom_photo2,
             'secteur'=> $this->input->post('secteur'),
             'ad'=> '0',
             'date_ins'=> time()
                    );
         $this->Inscription_m->update_inscription($iduser,$data);

         } }
         $user=$this->Inscription_m->get_adherent_by_id($iduser);
        $this->_data['user']=$user;
$this->display_view('home/compte');
    }

    function mot_de_passe(){
     is_adherent();
    $this->_data['page']="Mot de passe";
    $iduser=$this->session->userdata('userid');
    $info_user=$this->Inscription_m->get_adherent_by_id($iduser);
    $config = array(
            array('field' => 'apass',
                'rules' => 'required|md5'
            ),
            array('field' => 'npass',
                'rules' => 'required|matches[confirm_npass]'
            ),
            array('field' => 'confirm_npass',
                'rules' => 'required'
            )
        );
              $this->form_validation->set_rules($config);
                   if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {

                 if ($info_user->pass==$this->input->post('apass')) {

           $data = array( 'pass'=>md5($this->input->post('npass')) );
            $query = $this->Inscription_m->update_inscription($iduser,$data);
            $email=$info_user->email;
            $get_message= $this->Inscription_m->get_mail(4);
            $string=$this->input->post('npass');
             $message=$get_message["contenu"];
             $message=str_replace("%password%", $string, $message);
             mail($email, $get_message["titre"],$message , "From: contact@gwebcreation.com" );

             $this->_data['erreur'] = "Changement du mot de passe r&eacute;ussie!";
           } else $this->_data['erreur'] = "Mot de passe incorrect!";


            }}

     $this->display_view('home/pass');
    }
    function description(){
      is_adherent();
    $this->_data['page']="Mot de passe";
    $iduser=$this->session->userdata('userid');
   
     $config = array(
            array('field' => 'description',
                'rules' => 'required'
            )
        );
     $this->form_validation->set_rules($config);
                   if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
            $data = array( 'description'=>$this->input->post('description') );
            $query = $this->Inscription_m->update_inscription($iduser,$data);
             $this->_data['erreur'] = "Modification description r&eacute;ussie!";
            }}
             $info_user=$this->Inscription_m->get_adherent_by_id($iduser);
         $this->_data['user']=$info_user;
        $this->display_view('home/description');
    }
}

?>
